package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.TrapRequest;
import cc.rengu.igas.channel.cups.facade.response.TrapResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * 指定账户圈存
 *
 * @author Administrator
 */
public class CupsTrapService extends BasePayService<TrapRequest, TrapResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, TrapRequest trapRequest, TrapResponse trapResponse) throws Exception {
        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(trapRequest, cupsField);
        cupsField.setMsgType("0200");
        cupsField.setProcCode("600000");
        cupsField.setConCode("91");

        cupsField.setTransDateTime(trapRequest.getTransDateTime());
        cupsField.setLocalDate(trapRequest.getTransDateTime().substring(0, 4));
        cupsField.setLocalTime(trapRequest.getTransDateTime().substring(4));
        cupsField.setReferenceNo(cupsField.getLocalTime() + cupsField.getCupsTraceNo());

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        //  特殊计费类型
        cupsFieldBean.createF60()
                .setF60_2_2(trapRequest.getTermRead())
                .setF60_2_3(trapRequest.getIcConCode())
                .setF60_2_6(trapRequest.getNoPinFlag())
                .setF60_2_5(trapRequest.getTermType())
                .setF60_3_1(trapRequest.getSpecialBillType());
        cupsFieldBean.build();

        String tmpAmt = AmountUtil.changeY2F(trapRequest.getOrderAmt());
        cupsField.setOrderAmt(String.format("%012d", Integer.valueOf(tmpAmt)));
        cupsFieldBean.setCupsField(cupsField);
        ConvertUtil.convertInput(cupsFieldBean);


        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, trapRequest.getHeader().getTxnNum());
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, trapResponse);
        if (trapResponse.getSettleDate() != null) {
            trapResponse.setSettleDate(getCupsSettleDate(trapResponse.getSettleDate()));
        }
        trapResponse.setRespCode(cupsFieldRsp.getRespCode());
        if (CupsParamConstant.SUCCESS_CODE.equals(trapResponse.getRespCode())) {
            trapResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            trapResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(trapResponse);
    }
}
